﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace devtm.CommonInteractive.Config
{
    
    public class AnalyzeXml
    {


        private Func<XAttribute, bool> _testAttribute;
        private Func<XElement, bool> _testElement;


        public bool Visit(XElement xml, Func<XAttribute, bool> testAttribute = null, Func<XElement, bool> testElement = null)
        {

            _testAttribute = testAttribute;
            _testElement = testElement;

            return Visit(xml);

        }


        private bool Visit(XElement xml)
        {
            bool ret = true;

            if (_testElement != null)
                ret = _testElement(xml) && ret;

            ret = Visit(xml.Elements()) && ret;

            ret = Visit(xml.Attributes()) && ret;

            return ret;
        }

        public bool Visit(IEnumerable<XAttribute> attributes)
        {
            
            bool ret = true;

            foreach (XAttribute attr in attributes)
                if (_testAttribute != null)
                    ret = _testAttribute(attr) && ret;

            return ret;

        }

        public bool Visit(IEnumerable<XElement> xmls)
        {
            bool ret = true;

            foreach (XElement x in xmls)
                ret = Visit(x) && ret;

            return ret;
        }


    }

}
